package fs.ls.gs.dao;

import fs.ls.gs.Model.GoodsModel;
import fs.ls.gs.Service.GoodsService;
import fs.ls.gs.Utils.DbUtils;

import java.sql.*;
import java.util.List;
import java.util.Scanner;

/**
 * @Auther: zhezhe
 * @Date: 2018/9/1417:22
 * @Description:
 */
public class GoodsServiceImpl implements GoodsService {


   private Connection connection = DbUtils.getConnection();


        @Override
    public List<GoodsModel> queryAll() throws SQLException {


            //定义一个sql语句
            String sql = "SELECT * FROM goodstb;";
            Statement stm  = connection.createStatement();
            ResultSet rs = stm.executeQuery(sql);
            //展开结果集数据库


            //循环取得结果
            while(rs.next()){

                //拿出记录里面的内容，具体字段，用getXXX方法
                int id = rs.getInt("id");
                String name = rs.getString("goods_name");
                double price = rs.getDouble("goods_price");
                double num = rs.getDouble("goods_num");

                //拿到后输出数据
                System.out.print("ID: "+id);
                System.out.print(",名称: "+name+"，价格："+price + ",数量:" + num);
                System.out.println();
            }
            //后打开的先关
            DbUtils.closeStatement(stm);
            DbUtils.closeConnection(connection);
            System.out.println("GoodBye!");
        return null;
    }



    @Override
    public GoodsModel getGoodsId(int id) throws SQLException {
        String sql = "SELECT * FROM goodstb where id = "+id;
        PreparedStatement pstm  = connection.prepareStatement(sql) ;
        ResultSet rs = pstm.executeQuery(sql);
        GoodsModel goodsModel = null;
        //展开结果集数据库
        while(rs.next()){

            if (rs == null){
                System.out.println("无数据");
            }
            //通过字段检索
            String name = rs.getString("goods_name");
            double price = rs.getDouble("goods_price");
            Integer num = rs.getInt("goods_num");
            goodsModel = new GoodsModel(id, name, price, num);


        }
        DbUtils.closeStatement(pstm);
        DbUtils.closeConnection(connection);
        System.out.println("GoodBye!");
        return goodsModel;
    }

    @Override
    public GoodsModel getGoodsName(String name) throws SQLException {
        String sql = "SELECT * FROM goodstb where goods_name = "+name;
        PreparedStatement pstm  = connection.prepareStatement(sql) ;
        ResultSet rs = pstm.executeQuery(sql);
        GoodsModel goodsModel = null;
        //展开结果集数据库
        while(rs.next()){

            if (rs == null){
                System.out.println("无数据");
            }
            //通过字段检索
            int id = rs.getInt("id");
            double price = rs.getDouble("goods_price");
            Integer num = rs.getInt("goods_num");
            goodsModel = new GoodsModel(id, name, price, num);


        }
        DbUtils.closeStatement(pstm);
        DbUtils.closeConnection(connection);
        System.out.println("GoodBye!");
        return goodsModel;

    }

    @Override
    public Integer addGoods(GoodsModel goodsModel) {
        return null;
    }

    // public GoodsModel getGoodsName(String name) {
//        return null;
//    }
//
//    public Integer addGoods(GoodsModel goodsModel) {
//        Integer result = null;
//        try {
//            String sql1 = "INSERT INTO goodstb(goods_name,goods_price,goods_num) VALUES(?,?,?);";
//            PreparedStatement ps = null;
//            ps = connection.prepareStatement(sql1);
//            ps.setString(1, goodsModel.getGoodsName());
//            ps.setString(2, String.valueOf(goodsModel.getGoodsPrice()));
//            ps.setString(3, String.valueOf(goodsModel.getGoodsNum()));
//            result = ps.executeUpdate();
//            DbUtils.closepp(ps);
//            DbUtils.closeConnection(connection);
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
//        return result;
//    }
}





